package com.medata.utils;

import com.medata.dataObject.DefaultValueTable;
import com.medata.dataObject.Templatefields;
import com.medata.service.impl.DefaultValueTableServiceImpl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/5/13 0013 10:19
 * @description：
 */

public class TempfieldToXmlUtil {
    public static StringBuffer getXml(DefaultValueTableServiceImpl defaultValueTableService,String[] str, List<Templatefields> templatefields,Integer isSimple,String doctitle) {
        StringBuffer sb = new StringBuffer("");
        int title=0;
        for (String field : str) {
            field=field.substring(0,field.length()-1);
            String fieldname = field.indexOf(",")==0?field.substring(1, field.indexOf(":")):field.substring(0, field.indexOf(":"));;
            Iterator<Templatefields> tempIT = templatefields.iterator();
            while (tempIT.hasNext()) {
                Templatefields temp = tempIT.next();
                if (temp.getTpFieldDesc().equalsIgnoreCase(fieldname)){
                    if (field.indexOf(":")<field.length()){
                        String fileValue=(field.length()<(field.indexOf(":")+2))?"":field.substring(field.indexOf(":") + 2, field.length());
                        if (title==0&&doctitle!=null){
                            sb.append("<DispField FieldName=" + "\""+temp.getTpFieldName()+ "\""
                                    + " FieldIndex=\"1\"" + " FieldDesc=" + "\""+fieldname +"\""+ " InputType="
                                    + "\""+temp.getInputType() +"\""+ " RefID=" + "\""+temp.getRefId()+"\""
                                    + " DefaultValue=\"\"" + " DispFieldSel="+"\""+doctitle+"\""+ " IsNeed="+ "\""+temp.getIsNeed()+"\""+" NormID="+ "\""+temp.getNormId()+"\""+">" +fileValue
                                    + "</DispField>");
                            title++;
                        }else {
                            sb.append("<DispField FieldName=" + "\""+temp.getTpFieldName()+ "\""
                                    + " FieldIndex=\"1\"" + " FieldDesc=" + "\""+fieldname +"\""+ " InputType="
                                    + "\""+temp.getInputType() +"\""+ " RefID=" + "\""+temp.getRefId()+"\""
                                    + " DefaultValue=\"\"" +" DispFieldSel=\"\""+ " IsNeed="+ "\""+temp.getIsNeed()+"\""+" NormID="+ "\""+temp.getNormId()+"\""+">" +fileValue
                                    + "</DispField>");
                        }
                        if (isSimple==1&&temp.getRefId()!=0){
                            if (fileValue!=null&&!fileValue.equals("")){
                                DefaultValueTable valueTable = defaultValueTableService.findByDefValuesAndRefId(fileValue,temp.getRefId());
                                if (valueTable==null){
                                    DefaultValueTable defaultValueTable=new DefaultValueTable();
                                    defaultValueTable.setRefId(temp.getRefId());
                                    defaultValueTable.setDefValues(fileValue);
                                    defaultValueTableService.saveOrUpdate(defaultValueTable);
                                }
                            }
                        }
                        tempIT.remove();
                        break;
                    }
                }
            }
        }
        return sb;
    }

    public static StringBuffer getXml2(DefaultValueTableServiceImpl defaultValueTableService,String title,LinkedList<Templatefields> templatefieldsList,String[] fields){
        StringBuffer sb = new StringBuffer("");
        for (int i=0;i<fields.length;i++){
            String field = fields[i];
            if (field!=null&&field.length()>2){
                String[] keyvalue = field.split(":");
                String key = keyvalue[0];
                key = key.startsWith(",")?key.substring(1, key.length()):key.substring(0, key.length());
                String val = keyvalue[1];
                //20191022:zb:增加对:符号的支持
                if (keyvalue.length>2){
                    val=val+keyvalue[2];
                }
                String value="";
                if (val!=null&&val.length()>2){
                    value=val.substring(1,val.length()-1);
                }
                Templatefields temp = templatefieldsList.get(i);
                if (i==0){
//                    sb.append("<DispField FieldName=" + "\""+temp.getTpFieldName()+ "\""
//                            + " FieldIndex=\"1\"" + " FieldDesc=" + "\""+key +"\""+ " InputType="
//                            + "\""+temp.getInputType() +"\""+ " RefID=" + "\""+temp.getRefId()+"\""
//                            + " DefaultValue=\"\"" + " DispFieldSel="+"\""+title+"\""+ " IsNeed="+ "\""+temp.getIsNeed()+"\""+" NormID="+ "\""+temp.getNormId()+"\""+">" +value
//                            + "</DispField>");
                    //修复dispfieldsel值bug-->默认为空
                    sb.append("<DispField FieldName=" + "\""+temp.getTpFieldName()+ "\""
                            + " FieldIndex=\"1\"" + " FieldDesc=" + "\""+key +"\""+ " InputType="
                            + "\""+temp.getInputType() +"\""+ " RefID=" + "\""+temp.getRefId()+"\""
                            + " DefaultValue=\"\"" + " DispFieldSel="+"\""+title+"\""+ " IsNeed="+ "\""+temp.getIsNeed()+"\""+" NormID="+ "\""+temp.getNormId()+"\""+">" +value
                            + "</DispField>");
                }else {
                    sb.append("<DispField FieldName=" + "\""+temp.getTpFieldName()+ "\""
                            + " FieldIndex=\"1\"" + " FieldDesc=" + "\""+key +"\""+ " InputType="
                            + "\""+temp.getInputType() +"\""+ " RefID=" + "\""+temp.getRefId()+"\""
                            + " DefaultValue=\"\"" + " DispFieldSel=\"\""+ " IsNeed="+ "\""+temp.getIsNeed()+"\""+" NormID="+ "\""+temp.getNormId()+"\""+">" +value
                            + "</DispField>");
                }
                //将有关联的数据,放入数据库
                if (temp.getInputType()==0&&temp.getRefId()!=0&&!value.isEmpty()){
                    DefaultValueTable table = defaultValueTableService.findByDefValuesAndRefId(value, temp.getRefId());
                    if(table==null){
                        DefaultValueTable table1=new DefaultValueTable();
                        table1.setDefValues(value);
                        table1.setRefId(temp.getRefId());
                        defaultValueTableService.saveOrUpdate(table1);
                    }
                }
            }
        }
        return sb;
    }


    //test
    public static void main(String[] args) {
        String s = "客户类型:";
        String x = s.substring(s.indexOf(":")+2,s.length());
        System.out.println(x);
    }
}
